package com.tongle.frs.common.dto;

import com.tongle.frs.common.util.EnumUtils;
import org.springframework.util.Assert;

import java.io.Serializable;

public class ResultDTO<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private boolean success;
    private String errCode;
    private String errDesc;
    private T result;

    public ResultDTO<T> set(String code, String msg) {
        return set(code, msg, null);
    }

    public ResultDTO<T> set(String code, String msg, T result) {
        ResultCodeEnum codeEnum = (ResultCodeEnum) EnumUtils.getEnum(ResultCodeEnum.values(), "code", code);
        Assert.notNull(codeEnum);

        this.success = codeEnum.isSuccess();
        this.errCode = codeEnum.getCode();
        this.errDesc = msg;
        this.result = result;

        return this;
    }

    public ResultDTO<T> set(ResultCodeEnum resultEnum, String msg) {
        return set(resultEnum, msg, null);
    }

    public ResultDTO<T> set(ResultCodeEnum resultEnum, String msg, T result) {
        Assert.notNull(resultEnum);

        this.success = resultEnum.isSuccess();
        this.errCode = resultEnum.getCode();
        this.errDesc = (msg + "{" + resultEnum.getMsg() + "}");
        this.result = result;

        return this;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public String getErrCode() {
        return this.errCode;
    }

    public String getErrDesc() {
        return this.errDesc;
    }

    public T getResult() {
        return this.result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public String toString() {
        StringBuffer buf = new StringBuffer();
        buf.append("[success:" + this.success + ",errCode:").append(this.errCode);
        buf.append(",errDesc:").append(this.errDesc).append("],result:").append(this.result.toString());
        return buf.toString();
    }
}